package com.hyb.controller;


import com.alibaba.fastjson.JSON;
import com.hyb.Vo.OrderVo;
import com.hyb.entity.Order;
import com.hyb.page.PageReq;
import com.hyb.response.Result;
import com.hyb.service.OrderService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * <p>
 *  订单Controller
 * </p>
 *
 * @author hyb
 * @since 2021-12-29
 */
@RestController
@RequestMapping("/order")
public class OrderController {
    @Autowired
    private OrderService orderService;

    @RequestMapping(value = "/saveOrder",method = RequestMethod.POST)
    @ApiOperation("保存订单")
    public String saveOrder(@RequestBody List<OrderVo> orderVoList){
        orderService.createOrder(orderVoList);
        return JSON.toJSONString(new Result().setCode(200).setMessage("保存成功"));
    }

    @RequestMapping(value = "/loadOrders",method = RequestMethod.GET)
    @ApiOperation("加载状态不为已完成的订单")
    public String loadOrders(Integer limit,Integer current,Integer customerId){
         PageReq pageReq = orderService.loadOrders(limit, current, customerId);
         return JSON.toJSONString(new Result().setMessage("加载成功").setCode(200).setData(pageReq));
    }

    @RequestMapping(value = "/updateOrder",method = RequestMethod.PUT)
    @ApiOperation("修改订单")
    public String updateOrder(@RequestBody List<OrderVo> orderVoList){
        orderService.updateOrder(orderVoList);
        return JSON.toJSONString(new Result().setMessage("修改成功").setCode(200));
    }

    @RequestMapping(value = "/loadOrderByOrderId",method = RequestMethod.GET)
    @ApiOperation("根据订单Id获取订单状态")
    public String loadOrderByOrderId(Integer orderId){
        Order orderByOrderId = orderService.getOrderByOrderId(orderId);
        return JSON.toJSONString(new Result().setData(orderByOrderId).setMessage("加载成功").setCode(200));
    }

    @RequestMapping(value = "/updateOrderState",method = RequestMethod.PUT)
    @ApiOperation("更新订单")
    public String updateOrder(@RequestBody Order order){
        orderService.updateOrderState(order);
        return JSON.toJSONString(new Result().setCode(200).setMessage("修改成功"));
    }

    @RequestMapping(value = "/loadEndingOrders",method = RequestMethod.GET)
    @ApiOperation("加载已结束的订单")
    public String loadEndingOrders(Integer limit,Integer current,Integer customerId){
        PageReq pageReq = orderService.loadEndingOrders(limit, current, customerId);
        return JSON.toJSONString(new Result().setMessage("加载成功").setCode(200).setData(pageReq));
    }

    @RequestMapping(value = "/getOrderByOrderIdAndCustomerId",method = RequestMethod.GET)
    @ApiOperation("根据订单Id和客户Id加载订单信息")
    public String getOrderByOrderIdAndCustomerId(Integer orderId,Integer customerId){
        OrderVo orderByOrderIdAndCustomerId = orderService.getOrderByOrderIdAndCustomerId(orderId, customerId);
        return JSON.toJSONString(new Result().setData(orderByOrderIdAndCustomerId).setCode(200).setMessage("加载成功"));
    }

}
